/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2022. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * Description:
 * Author: huawei
 * Create: 2019-10-15
 */
#ifndef SVM_MEM_MNG_H
#define SVM_MEM_MNG_H

#include <linux/types.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include "svm_cmd.h"

/*
 * page property define for devmm_make_pgprot
 * bit0: mem readonly
 */
#define DEVMM_MEM_READONLY_BIT 0

#define DEVMM_PAGE_READONLY_FLG (1UL << DEVMM_MEM_READONLY_BIT)

typedef struct page *(*devmm_alloc_page_func)(int, u32, u32);
typedef int (*devmm_mem_split_inc_test_func)(u32 devid,
    u32 vfid, int nid, u64 page_num, int alloc_flag);

struct page_map_info {
    u32 devid;
    u32 vfid;
    u32 mem_type;
    u32 heap_type;
    int nid;
    u8 is_continuty;
    u8 is_svm;
    u8 page_prot;
    u64 va;
    u64 page_num;
    struct page **inpages;
};

struct devmm_mmap_para {
    u32 mmap_num;
    struct devmm_mmap_size mmap_size[DEVMM_MAX_VMA_NUM];
};

struct page *devmm_pa_to_page(u64 paddr);
void devmm_print_nodes_info(u32 devid, u32 vfid, u32 mem_type);
bool devmm_pa_is_remote_addr(u64 pa);

pgprot_t devmm_make_pgprot(unsigned int flg);
pgprot_t devmm_make_remote_pgprot(u32 flg);
pgprot_t devmm_make_nocache_pgprot(u32 flg);
pgprot_t devmm_make_reserved_pgprot(u32 flg);
bool devmm_is_readonly_mem(u32 page_prot);
int devmm_get_svm_pages_with_lock(void *svm_proc, u64 va, u64 num, struct page **pages);
struct vm_area_struct *devmm_find_vma_from_mm(struct mm_struct *mm, u64 vaddr);
struct vm_area_struct *devmm_find_vma_proc(struct mm_struct *mm, struct vm_area_struct *vma[],
    u32 vma_num, u64 vaddr);
u64 devmm_svm_mmap_get_max_read_only_addr(void);
u32 devmm_get_svm_vma_index(u64 vaddr, u32 vma_num);
void devmm_set_proc_vma(struct mm_struct *mm, struct vm_area_struct *vma[], u32 vma_num);
void devmm_init_dev_set_mmap_para(struct devmm_mmap_para *mmap_para);
void devmm_remove_vma_wirte_flag(struct vm_area_struct *vma);
int devmm_get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
    u64 va, u32 num, struct page **pages);

#endif /* SVM_MEM_MNG_H */
